﻿using BankingApp.Data;
using BankingApp.Models;
using BankingApp.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using BankingApp.Data.Models;
using BankingApp.Adapters;
using System.Data.Entity.Migrations;


namespace BankingApp.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        private IBankingAdapter _bankingAdapter;

        public HomeController()
        {
            _bankingAdapter = new AccountAdapter();
        }

        public HomeController(IBankingAdapter adapter)
        {
            _bankingAdapter = adapter;

        }
        public ActionResult Index()
        {
            string userId = User.Identity.GetUserId();
            UserAccountListViewModel model = new UserAccountListViewModel();
            model.Accounts = _bankingAdapter.GetAccount(userId);
            model.UserName = User.Identity.GetUserName();
           
            model.BankBalance = model.BankBalance;

            return View(model);
        }

        public ActionResult Create()
        {
            UserAccountViewModel model = new UserAccountViewModel();

            return View(model);
        }
        public ActionResult Create(UserAccountViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            string userId = User.Identity.GetUserId();
            _bankingAdapter.CreateAccount(model, userId);

            return RedirectToAction("Index");
        }
    }
}